//
//  CSGXCheckPreviewController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/22.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXCheckPreviewController.h"
#import "CSGXHomeSaleCategoryView.h"
#import "CSGXBaseTableView.h"

#import "CSGXInventoryCheckController.h"
#import "LKGXNumberModificationView.h"
#import "CSGXCheckPreviewCell.h"
#import "CSGXCheckPreviewFooter.h"

#import "CSTextField.h"
#import "RMCheckButton.h"

#import "CSGXCommodityPickerBLL.h"

#import "TInventoryModel.h"

static NSString * priviewCellId = @"priviewCellId";

@interface CSGXCheckPreviewController () <CSGXCategoryViewActionDelegate, UITableViewDataSource>

@property (nonatomic,weak) CSGXHomeSaleCategoryView * categoryView;

@property(nonatomic,weak) CSGXBaseTableView * tvDetail;

@property (nonatomic,weak) CSGXCheckPreviewFooter *footView;





@end

@implementation CSGXCheckPreviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self distribute];
}

- (void)viewWillDisappear:(BOOL)animated {
    self.isDashedModify = NO;
}

#pragma mark - methods
- (void)distribute {
    self.footView.checkPreviewFooterList = self.checkPreviewInventoryList;
    
    self.categoryView.bubbleDicts = @[@{@"bubbleNumber":@(self.checkPreviewInventoryList.count), @"bubbleIndex":@(0), @"bubbleColor":[UIColor orangeColor]}];
}


#pragma mark - actions

- (void)btnLeftAction:(id)sender {
    
    //    if (self.isDashedModify) {
    //        // alertVc
    //        UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"放弃修改？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    //        [alertVc addAction:[UIAlertAction actionWithTitle:@"放弃" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    //
    //            [self.navigationController popToViewController:[[CSHomeSearchSaleController alloc] init] animated:YES];
    //        }]];
    //        [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    //        [self presentViewController:alertVc animated:YES completion:nil];
    //
    //        return;
    //    }
    
    
    // alertVc
    UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"放弃编辑" message:@"确认放弃编辑退出？" preferredStyle:UIAlertControllerStyleAlert];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //call back
        [self.navigationController popToRootViewControllerAnimated:YES];
    }]];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)btnEditAction:(UIBarButtonItem *)btnEdit {
    CSGXInventoryCheckController * checkVc = self.navigationController.viewControllers[1];
    //    saleVc.isReceivedHome = YES;
    NSArray<TInventoryModel *> *checkedYesInventoryList = [CSGXCommodityPickerBLL getCheckedYesInventoryList:self.checkPreviewInventoryList];
    checkVc.checkInventoryList = checkedYesInventoryList;
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnScanAction:(UIBarButtonItem *)btnScan {
    
}

- (void)btnSearchAction:(UIBarButtonItem *)btnSearch {
    
}


#pragma mark - delegates
/// 点击了categoryView上的哪个按钮
- (void)gxCategoryView:(CSGXHomeSaleCategoryView *)categoryView withSelectedIndex:(NSInteger)selectedIdnex {
    
}


#pragma mark - tableView datasource and delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.checkPreviewInventoryList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CSGXCheckPreviewCell * cell = [tableView dequeueReusableCellWithIdentifier:priviewCellId];
    cell.checkPreviewInventoryModel = self.checkPreviewInventoryList[indexPath.row];
    cell.cellIndex = indexPath.row;
    
#pragma mark call back
    cell.callback_checkPreviewCell = ^{
        [self distribute];
    };
    return cell;
}


#pragma markl - setupUI
- (void)setNavBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"车销出库" style:UIBarButtonItemStylePlain target:self action:@selector(btnLeftAction:)];
    
    UIBarButtonItem * rightEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(btnEditAction:)];
    UIBarButtonItem * rightScan = [[UIBarButtonItem alloc] initWithTitle:@"扫描" style:UIBarButtonItemStylePlain target:self action:@selector(btnScanAction:)];
    UIBarButtonItem * rightSearch = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(btnSearchAction:)];
    
    self.navigationItem.rightBarButtonItems = @[rightEdit, rightScan, rightSearch];
}

- (void)setupUI {
    // navBar
    [self setNavBar];
    
    // CSGXHomeSaleCategoryView
    CSGXHomeSaleCategoryView * categoryView = [CSGXHomeSaleCategoryView gxCategoryViewWithDelegate:self page:1];
    [self.view addSubview:categoryView];
    self.categoryView = categoryView;
    
    [categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.leading.trailing.equalTo(self.view);
        make.height.offset(kHeightCategory * 2/3);
    }];
    categoryView.segments = @[@"正常库"];
    
    // tvDetail
    CSGXBaseTableView * tvDetail = [[CSGXBaseTableView alloc] init];
    [self.view addSubview:tvDetail];
    self.tvDetail = tvDetail;
    
    tvDetail.backgroundColor = [UIColor clearColor];
    
    [tvDetail registerClass:[CSGXCheckPreviewCell class] forCellReuseIdentifier:priviewCellId];
    tvDetail.dataSource = self;
    tvDetail.estimatedRowHeight = 130;
    tvDetail.rowHeight = UITableViewAutomaticDimension;
    
    [tvDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(categoryView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
    }];
    
    //remarkView
    UIView *vRemark = [[UIView alloc] init];
    [self.view addSubview:vRemark];
    
//    vRemark.backgroundColor = [UIColor orangeColor];
    
    [vRemark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tvDetail.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_offset(25);
    }];
    
    UILabel *lblCheck = [UILabel gx_labelWithText:@"打印小票" fontSize:12 color:[UIColor blackColor]];
    [vRemark addSubview:lblCheck];
    
    [lblCheck mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vRemark);
        make.right.equalTo(vRemark).offset(-5);
    }];
    
    RMCheckButton *btnCheck = [[RMCheckButton alloc] init];
    [vRemark addSubview:btnCheck];
    
    [btnCheck mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vRemark);
        make.right.equalTo(lblCheck.mas_left).offset(-5);
    }];
    [self.view layoutIfNeeded];
    
    CSTextField *txtRemark = [CSTextField gx_textFieldWithLineColor:[UIColor gx_colorWithRed:200 green:200 blue:200] lineWidth:2];
    [vRemark addSubview:txtRemark];
    
    [txtRemark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vRemark);
        make.left.equalTo(vRemark).offset(5);
        make.right.equalTo(btnCheck.mas_left).offset(-5);
    }];
    txtRemark.placeholder = @"添加备注";
    
    // footView
    CSGXCheckPreviewFooter * footView = [CSGXCheckPreviewFooter footerViewWithCallback:^{
#pragma mark footView call back
        UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"提交" message:@"确认提交吗？" preferredStyle:UIAlertControllerStyleAlert];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"提交" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            [CSGXProgressTool showMsgWithStatus:@"正在提交..." delay:-1];
            
            
            //提交到数据库
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [CSGXCommodityPickerBLL updateInventoryWithList:self.checkPreviewInventoryList]; //异步提交数据库
                [NSThread sleepForTimeInterval:1];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (1) {
                        //提交成功
                        [CSGXProgressTool showSuccessWithStatus:@"提交成功！" delay:0.5];
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                });
            });
        }]];
        [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        
        [self presentViewController:alertVc animated:YES completion:nil];

    }];
    [self.view addSubview:footView];
    self.footView = footView;
    
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vRemark.mas_bottom).mas_offset(2);
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.offset(60);
    }];
    
    [self.view layoutIfNeeded];
}



#pragma mark - setter & getter

- (void)setCheckPreviewInventoryList:(NSArray<TInventoryModel *> *)checkPreviewInventoryList {
    _checkPreviewInventoryList = checkPreviewInventoryList;
    [self distribute];
}



@end
